/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.stdlib;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * Implements all primitives from 'alloc.c'.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Alloc {

    /**
     * No instance of this class.
     */
    private Alloc() {
    } // end empty constructor

    /**
     * Creates a block with a tag equal to 0.
     * @param ctxt context
     * @param size size of block
     * @return a block of the given size, with tag 0 <br/>
     *         all values are initialized to <i>unit</i>
     */
    @Primitive
    public static Value caml_alloc_dummy(final CodeRunner ctxt,
                                         final Value size) {
        final int sz = size.asLong();
        if (sz == 0) {
            return ctxt.getContext().getAtom(0);
        } else {
            return Value.createFromBlock(Block.createBlock(sz, 0));
        } // end if/else
    } // end method 'caml_alloc_dummy(CodeRunner, Value)'

    /**
     * Creates a float block with a tag equal to 0.
     * @param ctxt context
     * @param size size of block
     * @return a block of the given size, with tag 0 <br/>
     *         all values are initialized to <i>0.0</i>
     */
    @Primitive
    public static Value caml_alloc_dummy_float(final CodeRunner ctxt,
                                               final Value size) {
        final int sz = size.asLong();
        if (sz == 0) {
            return ctxt.getContext().getAtom(0);
        } else {
            final Block res = Block.createDoubleArray(sz);
            res.setTag(0);
            return Value.createFromBlock(res);
        } // end if/else
    } // end method 'caml_alloc_dummy_float(CodeRunner, Value)'

    /**
     * Updates a block by copying the values from another one.
     * @param ctxt context
     * @param dummy copy destination
     * @param newval copy source
     * @return <i>unit</i>
     */
    @Primitive
    public static Value caml_update_dummy(final CodeRunner ctxt,
                                          final Value dummy,
                                          final Value newval) {
        final Block dummyBlock = dummy.asBlock();
        final Block newvalBlock = newval.asBlock();
        final int sz = newvalBlock.getWoSize();
        final int tag = newvalBlock.getTag();
        assert sz == dummyBlock.getWoSize() : "sizes should be equal";
        assert tag < Block.NO_SCAN_TAG || tag == Block.DOUBLE_ARRAY_TAG : "invalid tag";
        dummyBlock.setTag(tag);
        dummyBlock.copy(newvalBlock);
        return Value.UNIT;
    } // end method 'caml_update_dummy(CodeRunner, Value, Value)'

} // end class 'Alloc'
